<?php
/**
 * 成果model
 * User: guojing
 * Date: 2020/4/13
 * Time: 16:12
 */

namespace app\api\model;

use app\common\model\Attachment;
use think\Db;
use think\Model;

class Achievements extends Model
{

    protected $name = 'achievements';

    public static function addRecord($addData)
    {
        $data = $addData;
        $data['createtime'] = time();
        $data['updatetime'] = time();
        return self::create($data);
    }

    /**
     * 获取列表
     */
    public static function dataList($params = [])
    {
        $where = [];
        $page = '1, 10';
        if (isset($params['page']) && $params['page_size']) {
            $page = $params['page'] . ',' . $params['page_size'];
        }
        if (isset($params['uid'])) {
            $where['uid'] = $params['uid'];
        }
        if (isset($params['project_id'])) {
            $where['project_id'] = $params['project_id'];
        }
        $list = self::where($where)->page($page)->select();
        $listData = [];
        if ($list) {
            foreach ($list as $listVal) {
                // 发布时间
                $listVal['publish_time'] = date('Y-m-d', $listVal['publish_time']);
                //获取类别
                $type = AchievementsType::findInfoByCondition(['id' => $listVal['type_id']]);
                $listVal['type_name'] = $type && isset($type['name']) ? $type['name'] : '';

                //上传的文件
                $listVal['file_name'] = '';
                $listVal['file_link'] = '';
                if ($listVal['attach_id']) {
                    $attach = Attachment::findInfoByCondition(['id' => $listVal['attach_id']]);
                    if ($attach) {
                        $listVal['file_link'] = $attach['url'];
                        $extraParams = $attach['extparam'] ? json_decode($attach['extparam'], true) : [];
                        $listVal['file_name'] = $extraParams && isset($extraParams['filename']) ? $extraParams['filename'] : '';
                }
                }

                $listData[] = $listVal;
            }
        }
        return $listData;
    }

}